<?php
/*
  [EWEISHOP] Copyright(C) 2017 Elinkint Inc.
  This is NOT a freeware, use is subject to license terms
   
  OfficialAccountMenu.php 2018-05-24 17:56 likexin
   
*/

namespace common\library\wechat\officialaccount;

use common\library\wechat\WechatApi;
use common\component\wechat\Wechat;

class OfficialAccountMenu extends WechatApi
{

    /**
     * 获取列表
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/menu
     */
    public static function getList()
    {
        $result = self::getInstance()->menu->list();
        return Wechat::apiError($result);
    }

    /**
     * 获取当前菜单
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/menu
     */
    public static function getCurrent()
    {
        $result = self::getInstance()->menu->current();
        return Wechat::apiError($result);
    }

    /**
     * 添加普通菜单
     * @param array $buttons 菜单
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/menu
     * @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013
     */
    public static function createDefault(array $buttons)
    {
        $result = self::getInstance()->menu->create($buttons);
        return Wechat::apiError($result);
    }

    /**
     * 添加个性化菜单
     * @param array $buttons 菜单
     * @param array $matchRule 匹配规则
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/menu
     */
    public static function create(array $buttons, array $matchRule)
    {
        $result = self::getInstance()->menu->create($buttons, $matchRule);
        return Wechat::apiError($result);
    }

    /**
     * 删除菜单
     * @param string $menuId 菜单ID 为all时删除全部
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/menu
     */
    public static function delete(string $menuId = null)
    {
        if(empty($menuId)) {
            return error('menuId错误');
        }
        elseif($menuId == 'all') {
            $menuId = '';
            $result = self::getInstance()->menu->delete();
        }else{
            $result = self::getInstance()->menu->delete($menuId);
        }
        return Wechat::apiError($result);
    }

    /**
     * 测试个性化菜单
     * @param string $userId 可以是OPENID, 也可是微信号
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/menu
     */
    public static function match(string $userId)
    {
        $result = self::getInstance()->menu->match($userId);
        return Wechat::apiError($result);
    }

}